﻿@using Huirui.Cavan.Domain.MainBoundedContext.SystemModule
@model Huirui.Cavan.Presentation.ViewModel.SystemModule.IDepartmentViewModel
@{Layout = null;}
@using (Html.BeginForm(ActionConst.ADD, ControllerConst.DEPARTMENT, FormMethod.Post))
{
    <div class="tool_bar">
        <input id="dept_save" type="submit" style="display: none;" />
        <input id="dept_reset" type="reset" style="display: none" />
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:$('#dept_save').click()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-copy'">复制</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'" id="departmentReset">重置</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'" onclick="toggleHeard()">折叠/展开</a>
    </div>
    <table class="form_heard">
        <tr>
            <td class="c100">
                <label class="BlueRequired">编号:</label>
            </td>
            <td class="c160">
                @{
    var createTypeList = new List<SelectListItem>();
    createTypeList.Add(new SelectListItem() { Selected = true, Text = "自动生成", Value = "1" });
    createTypeList.Add(new SelectListItem() { Text = "手工输入", Value = "0" });
                }
                <table class="form_heard" style="border-collapse: collapse">
                    <tr>
                        <td class="c80" style="padding: 0">
                            @Html.DropDownList("departmentIdCreateType", createTypeList)
                        </td>
                        <td class="c80">
                            @Html.TextBoxFor(m => m.NoPrefixId, new { id = "department_NoPrefixId" })
                            @Html.HiddenFor(m => m.Item.Id, new { id = "department_Id" })
                        </td>
                    </tr>
                </table>
            </td>
            <td class="c100">
                <label class="BlueRequired">名称:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Name, new { id = "department_name" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>上级部门:</label>
            </td>
            <td class="c160">
                @Html.TextBox("sueriorDepartmentName", "", new { id = "superiorDepartmentNameForEditDepartment" })
                @Html.HiddenFor(m => m.Item.SuperiorDepartment, new { id = "superiorDepartmentForEditDepartment" })
            </td>
            <td class="c100">
                <label>备注:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Remark, new { id = "department_remark" })
            </td>
        </tr>
    </table>
}
<table id="department_dg">
    <thead>
        <tr>
            <th data-options="field:'ck',checkbox:'true',width:10"></th>
            <th data-options="field:'NoPrefixId',width:100">编号</th>
            <th data-options="field:'Name',width:100">名称</th>
            <th data-options="field:'SuperiorDepartment',width:100">上级部门</th>
            <th data-options="field:'Remark',width:100">备注</th>
        </tr>
    </thead>
</table>
<div id="departmenttb">
    <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" id="deleteDepartment">选择删除</a>
    <a href="#" class="easyui-linkbutton" iconcls="icon-excel" plain="true" onclick="javascript:alert('引出')">引出</a>
</div>
@{ Html.RenderAction(ActionConst.SEARCH, ControllerConst.DEPARTMENT, new { purpose = "ForEditDepartment" }); }
<script type="text/javascript">
    $("#department_NoPrefixId").attr("disabled", true);
    $(function ()
    {
        $("form[action^='/department']").submit(function () {
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data) {
                    if (data.IsSuccess) {
                        $('#department_dg').datagrid('reload');
                    }
                },
                error: function () {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        $('#department_dg').datagrid({
            class: 'easyui-datagrid',
            title: '部门列表',
            idfield: 'ID',
            singleSelect: 'true',
            selectOnCheck: 'true',
            fitColumns: 'true',
            pagination: 'true',
            toolbar: '#departmenttb',
            url: '/department/list',
            onSelect: function (rowIndex, rowData) {
                $("#department_NoPrefixId").val(rowData.NoPrefixId).attr("readonly", true);
                $("#department_Id").val(rowData.Id);
                $("#department_name").val(rowData.Name);
                $("#department_remark").val(rowData.Remark);
                $("#superiorDepartmentNameForEditDepartment").val(rowData.SuperiorDepartment);
            }
        });

        $("#superiorDepartmentNameForEditDepartment").bind("click", function () {
            InitSelectDepartment('departmentsForEditDepartment');
            openWindow("selectDepartmentWindowForEditDepartment");
        });

        $("#searchDepartmentForEditDepartment").submit(function () {
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    if (result.total > 0) {
                        $('#departmentsForEditDepartment').datagrid({
                            data: result,
                            checkOnSelect: true,
                            selectOnCheck: true
                        });
                    }
                },
                error: function () {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        $("#doSelectDepartmentForEditDepartment").bind("click", function () {
            var currentRow = $('#departmentsForEditDepartment').datagrid('getSelected');
            $("#superiorDepartmentForEditDepartment").val(currentRow.Id);
            $("#superiorDepartmentNameForEditDepartment").val(currentRow.Name);
            closeWindow("selectDepartmentWindowForEditDepartment");
        });

        $("#deleteDepartment").bind("click", function () {
            if ($('#department_dg').datagrid('getChecked').length) {
                var selected = $('#department_dg').datagrid('getChecked');

                var effectRow = new Object();
                if (selected.length) {
                    effectRow["selected"] = JSON.stringify(selected);
                }

                $.post("/department/delete", effectRow, function (data) {
                    if (data.IsSuccess) {
                        $('#department_dg').datagrid('reload');
                        $('#department_dg').datagrid('clearChecked');
                    }
                }, "JSON").error(function () {
                    $.messager.alert("提示", "网络异常，请稍候再试");
                });
            }
        });

        $("#departmentReset").bind("click", function () {
            $('#dept_reset').click();
            if ($('select#departmentIdCreateType').val() == "1")
            {
                $("#department_NoPrefixId").attr("disabled", true);
            }
            else
            {
                $("#department_NoPrefixId").attr("disabled", false);
            }
        });

        $("select#departmentIdCreateType").bind("change", function () {
            if ($(this).val() == "1") {
                $("#department_NoPrefixId").attr("disabled", true);
            }
            else {
                $("#department_NoPrefixId").attr("disabled", false);
            }
        });
    });


</script>
